<?php
require_once '../libs/app.config.php';
session_start ();
if ($_SESSION ['LoginId'] == false) {
	header ( "Location:login.php" );
}


$tpn = new Page ();

if ($_REQUEST ['searchId']) {
	$searchId = $_REQUEST ['searchId'];
	
	// 分类列表
	$sql = "select recordId, time, location, detail from order_records where orderId = '" . $_REQUEST ['searchId'] . "' order by time";
	$query = $tpm->query ( $sql );
	$totals = $tpm->num_rows ( $query );
	$tpn->pageft ( $totals, 15 );
	$result = $tpm->query ( $sql . " limit " . $tpn->getfirstcount () . ",15" );
	
	while ( $rows = $tpm->fetch_array ( $result ) ) {
		$record_list [] = array (
				'recordId' => $rows ['recordId'],
				'time' => $rows ['time'],
				'location' => $rows ['location'],
				'detail' => $rows ['detail'] 
		);
	}
	
	$sql2 = "select id, origin, destination, items, status, receiver from orders where orderId = '" . $_REQUEST ['searchId'] . "'";
	$query2 = $tpm->query ( $sql2 );
	$rows = $tpm->fetch_array ( $query2 );
	$order_details = array (
			"id" => $rows ['id'],
			"origin" => $rows ['origin'],
			"destination" => $rows ['destination'],
			"items" => $rows ['items'],
			"status" => $rows ['status'],
			"receiver" => $rows ['receiver'] 
	);
}

// 添加新Records
if ($_POST ['submit'] && $_GET ['act'] == 'add') {
	$add = array (
			"orderId" => $searchId,
			"time" => $time,
			"location" => $location,
			"detail" => $detail 
	);
	
	$result = $tpm->insert ( "order_records", $add );
	if ($result > 0)
		echo "<script>alert('Add successfully!');location.href=('order_records.php?searchId={$searchId}');</script>";
}

// 单项删除
if ($_POST ["delid"]) {
	$sql = "delete from order_records where recordId= '" . $_POST ['delid'] . "'";
	$query = $tpm->query ( $sql );
	if ($query > 0)
		echo "1";
	else
		echo "0";
}

if ($_POST ['completeId']) {
	
	$id = $_POST ['completeId'];
	$sql3 = "update orders set status = 'completed', receiver = '".$order_details.receiver."' where id = '" . $id . "'";
	$query1 = $tpm->query ( $sql3 );
	if ($query > 0)
		echo "<script>alert('Order Completed!');location.href=('ongoing_orders_list.php');</script>";
	else
		echo "0";
}




require_once (INCLUDE_PATH . 'template.inc.php'); // 模板类及对象$tpl的加载
$tpl->assign ( "record_list", $record_list );
$tpl->assign ( "order_details", $order_details );
$tpl->assign ( "searchId", $searchId );
$tpl->assign ( 'pages', $tpn->getpagenav () );
$tpl->display ( "order_records.html" );
?>